package org.kitten.core.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil {
    private static final Log logger = LogFactory.getLog(DateUtil.class);
	public final static int HOUR = 60 * 60 * 1000;
	public final static int MIN = 60 * 1000;
	public final static int SEC = 1000;

	private DateUtil() {
	}

	public static Timestamp getTimestamp() {
		return new Timestamp(new Date().getTime());
	}

	public static java.sql.Date getJavaSqlDate() {
		return new java.sql.Date(new Date().getTime());
	}

	public static String getFormatedDate(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static String getFormatedDate(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static Date getYesterdayZeroOclock() {
		return DateUtils.add(getTodayZeroOclock(), Calendar.DATE, -1);
	}

	public static Date getTodayZeroOclock() {
		return DateUtils.truncate(new Date(), Calendar.DATE);
	}

	public static Date getTomorrowZeroOclock() {
		return DateUtils.add(getTodayZeroOclock(), Calendar.DATE, 1);
	}

	public static Date getFirstDateOfThisMonth() {
		return DateUtils.truncate(new Date(), Calendar.MONTH);
	}

	public static Date getFirstDateOfNextMonth() {
		return DateUtils.add(getFirstDateOfThisMonth(), Calendar.MONTH, 1);
	}

	public static int getDateDiff(Date startDate, Date endDate) {
		int i = (int) ((endDate.getTime() - startDate.getTime()) / 3600 / 24 / 1000);
		return Math.abs(i);

	}

	public static int getDateDiff(java.sql.Date startDate, java.sql.Date endDate) {
		int i = (int) ((endDate.getTime() - startDate.getTime()) / 3600 / 24 / 1000);
		return Math.abs(i);

	}

	public static int getDateDiff(String startDate, String endDate) {
		int i = (int) ((java.sql.Date.valueOf(endDate).getTime() - java.sql.Date
				.valueOf(startDate).getTime()) / 3600 / 24 / 1000);
		return Math.abs(i);

	}

	/**
	 * 取得两个日期之前间隔的天数
	 * 
	 * @param small
	 * @param big
	 * @param format
	 * @return
	 */
	public static long getDayDiff(String small, String big, String format) {
		long day = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date date1 = sdf.parse(small);
			Date date2 = sdf.parse(big);

			// 日期相减得到相差的日期
			day = (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return Math.abs(day);
	}

	public static Date ymdToDate(String strDate, String mask) {
		if (strDate != null && mask != null) {
			String defaultCentury = String.valueOf(
					Calendar.getInstance().get(Calendar.YEAR)).substring(0, 2);
			String[] masks = mask.split(",");
			int y_i = strDate.lastIndexOf(masks[0]); // yy
			int M_i = strDate.lastIndexOf(masks[1]);
			int d_i = strDate.lastIndexOf(masks[2]);
			int y = 0, M = 1, d = 0;

			if (y_i != -1) {
				y = Integer.parseInt(strDate.substring(0, y_i));
			}
			if (String.valueOf(y).length() == 2) {
				y = Integer.parseInt(defaultCentury) * 100 + y;
			}
			if (M_i != -1) {
				M = Integer.parseInt(strDate.substring(y_i + masks[0].length(),
						M_i));
			}
			if (d_i != -1) {
				d = Integer.parseInt(strDate.substring(M_i + masks[1].length(),
						d_i));
			}
			Calendar c = Calendar.getInstance();
			c.set(y, M - 1, d);
			return c.getTime();
		} else {
			return null;
		}
	}

	public static Date strToDate(String strDate, String mask) {
		if (strDate != null && mask != null
				&& strDate.length() == mask.length()) {
			String defaultCentury = String.valueOf(
					Calendar.getInstance().get(Calendar.YEAR)).substring(0, 2);
			int y_i_1 = mask.lastIndexOf("yy"); // yy
			int y_i_2 = mask.lastIndexOf("yyyy"); // yyyy
			int M_i = mask.indexOf("MM");
			int d_i = mask.indexOf("dd");
			int h_i = mask.indexOf("hh");
			int m_i = mask.indexOf("mm");
			int s_i = mask.indexOf("ss");
			int y = 0, M = 1, d = 0, h = 0, m = 0, s = 0;

			// yy
			if (y_i_1 != -1) {
				y = Integer.parseInt(defaultCentury
						+ strDate.substring(y_i_1, y_i_1 + 2));
			}
			// yyyy
			if (y_i_2 != -1) {
				y = Integer.parseInt(strDate.substring(y_i_2, y_i_2 + 4));
			}
			if (M_i != -1) {
				M = Integer.parseInt(strDate.substring(M_i, M_i + 2));
			}
			if (d_i != -1) {
				d = Integer.parseInt(strDate.substring(d_i, d_i + 2));
			}
			if (h_i != -1) {
				h = Integer.parseInt(strDate.substring(h_i, h_i + 2));
			}
			if (m_i != -1) {
				m = Integer.parseInt(strDate.substring(m_i, m_i + 2));
			}
			if (s_i != -1) {
				s = Integer.parseInt(strDate.substring(s_i, s_i + 2));
			}
			Calendar c = Calendar.getInstance();
			c.set(y, M - 1, d, h, m, s);
			return c.getTime();
		} else {
			return null;
		}
	}

	public static boolean isBetween(String startDate, String mask1,
			String endDate, String mask2) {
		String curr_date = DateUtil.getFormatedDate("yyyyMMdd");
		String star_date = DateUtil.getFormatedDate(DateUtil.ymdToDate(
				startDate, mask1), "yyyyMMdd");
		String end_date = DateUtil.getFormatedDate(DateUtil.ymdToDate(endDate,
				mask2), "yyyyMMdd");
		if (curr_date.compareTo(star_date) < 0
				|| curr_date.compareTo(end_date) > 0) {
			return false;
		}
		return true;
	}

	public static Timestamp strToTimestamp(String strDate, String mask) {
		return new Timestamp(strToDate(strDate, mask).getTime());
	}

	public static int strToTimeMillis(String str) {
		int interval = 24 * HOUR;
		if (str.endsWith("h")) {
			interval = Integer.parseInt(str.substring(0, str.indexOf("h")))
					* HOUR;
		} else if (str.endsWith("m")) {
			interval = Integer.parseInt(str.substring(0, str.indexOf("m")))
					* MIN;
		} else if (str.endsWith("s")) {
			interval = Integer.parseInt(str.substring(0, str.indexOf("s")))
					* SEC;
		} else if (str.endsWith("ms")) {
			interval = Integer.parseInt(str.substring(0, str.indexOf("ms")));
		} else {
			logger.warn("invalid time str:" + str);
		}
		return interval;
	}

	public static void main(String[] args) {
		int i = DateUtil.getDateDiff(java.sql.Date.valueOf("2008-01-01"),
				java.sql.Date.valueOf("2008-01-01"));
		System.out.println(i);
	}
}
